package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwTongrongMapper;
import com.zjdiepu.www.ibiz.yw.IYwTongrongService;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwTongrong;
import com.zjdiepu.www.model.yw.ext.YwTongrongExt;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * 通融申请Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 09时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwTongrongServiceImpl extends BaseService implements IYwTongrongService {
	public static final Logger logger = LoggerFactory.getLogger(YwTongrongServiceImpl.class);

	@Autowired
	private YwTongrongMapper ywTongrongMapper;
	
	@Override
	public void saveUpdateAndDelete(YwTongrong ywTongrong, OperateType operType) {
		switch (operType) {
		case add:
			ywTongrong.initInsertData(ywTongrong.getModifier(), ywTongrong.getModifierName());
			ywTongrongMapper.insert(ywTongrong);
			break;
		case edit:
			ywTongrong.initUpdateData(ywTongrong.getModifier(), ywTongrong.getModifierName());
			// 影响的记录数
			int updateCount = ywTongrongMapper.updateByPrimaryKeySelective(ywTongrong);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywTongrong.initDeleteData(ywTongrong.getModifier(), ywTongrong.getModifierName());
			// 影响的记录数
			int deleteCount = ywTongrongMapper.updateByPrimaryKeySelective(ywTongrong);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwTongrong> selectByExt(YwTongrongExt ywTongrongExt) {
		return ywTongrongMapper.selectByExt(ywTongrongExt);
	}
	
	@Override
	public int countByExt(YwTongrongExt ywTongrongExt){
		return ywTongrongMapper.countByExt(ywTongrongExt);
	}
	
	@Override
	public YwTongrong selectById(String id) {
		return ywTongrongMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywTongrongMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwTongrong ywTongrong) {
		return ywTongrongMapper.countRefined(ywTongrong);
	}

	@Override
	public YwTongrong selectRefined(YwTongrong ywTongrong) {
		return ywTongrongMapper.selectRefined(ywTongrong);
	}
	
	@Override
	public int deletedRefined(YwTongrong ywTongrong) {
		return ywTongrongMapper.deletedRefined(ywTongrong);
	}

	@Override
	public boolean repeat(YwTongrong ywTongrong) {
		int count = countRefined(ywTongrong);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywTongrong.getId())) {
			return true;
		}
		YwTongrong dbItem = selectRefined(ywTongrong);
		if(null == dbItem) {
			return false;
		}
		return !ywTongrong.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywTongrongMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwTongrong> ywTongrongs) {
		return ywTongrongMapper.insertBatch(ywTongrongs);
	}

	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return ywTongrongMapper.countMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<YwBusiness> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return ywTongrongMapper.selectMultiByExt(ywBusinessQueryRequest);
	}
}
